// 引入 gulp及组件
var gulp=require('gulp'),  //gulp基础库
minifycss=require('gulp-minify-css'),   //css压缩
concat=require('gulp-concat'),   //合并文件
uglify=require('gulp-uglify'),   //js压缩
rename=require('gulp-rename'),   //文件重命名
jshint=require('gulp-jshint'),   //js检查
notify=require('gulp-notify'),   //提示
gulpless = require('gulp-less');  //less转css
sourcemaps = require('gulp-sourcemaps');
plumber = require('gulp-plumber'); //处理出现异常并不终止watch事件（gulp-plumber）


//css处理
gulp.task('mcss',function(){
return gulp.src('scc/css/*.css')      //设置css
// gulp.src(['src/less/index.less','src/less/detail.less'])
   .pipe(concat('order_query.css'))      //合并css文件到"order_query"
   .pipe(gulp.dest('dist/css'))           //设置输出路径
   .pipe(rename({suffix:'.min'}))         //修改文件名
   .pipe(minifycss())                    //压缩文件
   .pipe(gulp.dest('dist/styles'))            //输出文件目录
   .pipe(notify({message:'css task ok'}));   //提示成功
});

//JS处理
gulp.task('mjs',function(){
return gulp.src(['src/js/*.js'])  //选择合并的JS
   .pipe(concat('order_query.js'))   //合并js
   .pipe(gulp.dest('dist/js'))         //输出
   .pipe(rename({suffix:'.min'}))     //重命名
   .pipe(uglify())                    //压缩
   .pipe(gulp.dest('dist/js'))            //输出 
   .pipe(notify({message:"js task ok"}));    //提示
});

gulp.task('mless', function(){
    gulp.src('src/less/**/*.less')
        // .pipe(gulpSourcemaps.init())//sourcemaps
        .pipe(gulpless())//编译less
        // .pipe(gulpSourcemaps.write())
        .pipe(gulp.dest('src/lesscss'))
        .pipe(concat('order_query.css'))
        .pipe(rename({suffix:'.min'}))
        .pipe(minifycss())//压缩css
        // .pipe(gulpSourcemaps.write())
        .pipe(gulp.dest('dist/css/'));
});


gulp.task('testLess', function () {
    gulp.src('src/less/*.less')
        .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
        .pipe(less())
        .pipe(gulp.dest('src/css'));
});

gulp.task('testWatch', function () {
    gulp.watch('src/**/*.less', ['testLess']); //当所有less文件发生改变时，调用testLess任务
});

gulp.task('default', ['mcss', 'mjs']);


